﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDHDeepTunnel.EquipmentManagement.Interfaces;
using DDHDeepTunnel.EquipmentManagement.Data;
using NativeDataOperation.Interfaces;
using WebFoundation;
using WebFoundation.Data;

namespace DDHDeepTunnel.EquipmentManagement.Business
{
    internal class Batch : IBatch
    {
        private DbManager _queryDbManager;
        private DbManager _writeDbManager;
        public Batch(DbManager queryDbManager, DbManager writeDbManager, OnlineDataModule module)
        {
            this._queryDbManager = queryDbManager;
            this._writeDbManager = writeDbManager;
        }

        #region Batch Findings

        //public string InsertBatchDataTable(List<SqlPropertyMap> sqlPropertyCreateTable, List<SqlPropertyMap> sqlPropertyBatchTable, string tableName, DataTable dataTable)
        //{
        //    var stringReturn = "";

        //    return stringReturn;
        //}

        #endregion

        public string InsertBatch<T>(List<T> entityList) where T : class
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlBatchWritingDAO<T>(_writeDbManager)).InsertBatch<T>(entityList);
        }

        public string InsertBatchDataTable(DataTable dataTable, List<SqlPropertyMap> properties, string tableName)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).InsertBatchDataTable(dataTable, properties, tableName);
        }

        public string InsertBatchSqlDataTable(DataTable dataTable, List<SqlPropertyMap> properties, string tableName, int batchSize = 10000)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).InsertBatchSqlDataTable(dataTable, properties, tableName, batchSize);
        }

        public string InsertBatchGISSqlDataTable(DataTable dataTable, List<SqlPropertyMap> properties, string tableName, int batchSize = 10000)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).InsertBatchSqlDataTable(dataTable, properties, tableName, batchSize);
        }

        public bool InsertBatchDictionary(string sqlString, string destinationTableName,
            Dictionary<string, string> hashArrayParam,
            Dictionary<string, object> hashParamValue, int arrayBindCount)
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlTableWritingDAO(_writeDbManager)).InsertBatchDictionary(sqlString, destinationTableName, hashArrayParam, hashParamValue, arrayBindCount);
        }

        public string UpdateBatch<T>(IEnumerable<T> entityList) where T : class
        {
            try
            {
                var connString = _writeDbManager.ConnectionStr;
                var databaseType = _writeDbManager.DBType;
                if (_writeDbManager != null)
                {
                    _writeDbManager.Dispose();
                }
                _writeDbManager = new DbManager(connString, databaseType);
            }
            catch (Exception ex)
            {
                this.GetType().LogDebug("DBManager connection failed. Error: " + ex.Message);
            }
            return (new SqlBatchWritingDAO<T>(_writeDbManager)).UpdateBatch(entityList);
        }
    }
}